// pages/main/main.js
var wxCharts = require('../../utils/wxcharts-min.js');
var app = getApp();
var lineChart = null;
var startPos = null;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    numberS: 100,
    point: {},
    xuezhi_data: {},
    xuetang_data: {},
    checkDate: '',
    point: {}
  },
  touchHandler: function (e) {
    var that = this;
    var userInfo = wx.getStorageSync('trUserInfo');
    lineChart.showToolTip(e, {
      // background: '#7cb5ec',
      format: function (item, category) {
        that.setData({
          numberS: item.data,
          checkDate: category
        })
        wx.request({
          url: getApp().globalData.host_url + '/medicalExReport/ajax_showBloodReportMain?userId=' + userInfo.userId + '&checkDate=' + category,
          method: 'GET',
          data: {},
          success: function (res) {
            var listCheckDate = res.data.listCheckDate;
            if (listCheckDate == null) {
              var textToast = "暂无健康简报信息！";
              wx.showToast({
                title: textToast,
                icon: '',
                duration: 2000
              })
            } else {
              var medicalData;
              that.setData({
                xuezhi_data: res.data.xuezhi_data,
                xuetang_data: res.data.xuetang_data,
                point: {
                  name: category,
                  x: e.changedTouches[0].x - 25
                }
              })
            }
          }
        })
        return category + ' ' + item.name + ':' + item.data
      }
    });
  },
  onLoad: function (e) {
    var userInfo = wx.getStorageSync('trUserInfo');
    var that = this;
    var windowWidth = 320;
    var listCheckDate = [];
    var listscore = [];
    try {
      var res = wx.getSystemInfoSync();
      windowWidth = res.windowWidth;
    } catch (e) {
      console.error('getSystemInfoSync failed!');
    }

    wx.request({
      url: getApp().globalData.host_url + '/medicalExReport/ajax_showBloodReportMain?userId=' + userInfo.userId,
      method: 'GET',
      data: {},
      success: function (res) {
        listCheckDate = res.data.listCheckDate;
        listscore = res.data.listscore;
        if (listCheckDate == null) {
          var textToast = "暂无健康简报信息！";
          wx.showToast({
            title: textToast,
            icon: '',
            duration: 2000
          })
        } else {
          var medicalData;
          that.setData({
            xuezhi_data: res.data.xuezhi_data,
            xuetang_data: res.data.xuetang_data,
            numberS: res.data.numberS,
            checkDate: res.data.showCheckDate,
            point: {
              name: res.data.showCheckDate
            }
          })
          lineChart = new wxCharts({
            canvasId: 'areaCanvas',
            type: 'area',
            categories: listCheckDate,
            animation: false,
            background: 'transparent',
            series: [{
              name: '评分',
              data: listscore
            }],
            xAxis: {
              disableGrid: true,
              fontColor: 'transparent'
            },
            yAxis: {
              disabled: false,
              title: false,
              gridColor: 'transparent',
              format: function (val) {
                return '';
              },
              min: 0
            },
            width: windowWidth,
            height: 120,
            dataLabel: true,
            dataPointShape: true,//是否在图标上显示数据点标志
            enableScroll: false,
            legend: false,
            dataLabel: false
          });
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})